package top.yfq.multidatasource.service;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import top.yfq.multidatasource.datasource.AbstractDataSourceService;
import top.yfq.multidatasource.datasource.DataSourceInfo;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yfq
 * @date 2025/9/27 11:48
 * @description
 */
@Service
public class DataSourceService extends AbstractDataSourceService {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void init() {
        List<DataSourceInfo> dataSourceInfos = jdbcTemplate.query("select * from sys_tenant", (rs, rowNum) -> DataSourceInfo.builder()
                .key(rs.getString("id"))
                .type(rs.getString("datasource_type"))
                .jdbcUrl(rs.getString("jdbc_url"))
                .username(rs.getString("username"))
                .password(rs.getString("password"))
                .build());

        for (DataSourceInfo dataSourceInfo : dataSourceInfos) {
            dataSourceInfoMap.put(dataSourceInfo.getKey(), dataSourceInfo);
        }
    }
}
